package com.bestcem.xm.user.grpc.service.converter;

import cn.hutool.core.util.StrUtil;
import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.user.entity.pojo.UserDO;
import com.bestcem.xm.user.enums.BooleanStatusEnum;
import com.bestcem.xm.user.enums.UserAccpectFlagEnum;
import com.bestcem.xm.user.enums.UserInnerFlagEnum;
import com.bestcem.xm.user.service.dto.user.UserConditionDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * UserCondition 转换类
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/30 16:26
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserConditionConvert {
    /**
     * userCondition to UserConditionDTO
     *
     * @param userCondition 用户查询条件msg
     * @return 用户查询条件DTO
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 17:21 2022/6/30
     */
    public static UserConditionDTO msg2Dto(UserCondition userCondition) {
        UserConditionDTO userConditionDTO = new UserConditionDTO();
        userConditionDTO.setUserId(userCondition.getId());
        userConditionDTO.setOrgId(userCondition.getOrgId());
        userConditionDTO.setUserName(userCondition.getUsername());
        userConditionDTO.setPassword(userCondition.getPassword());
        userConditionDTO.setName(userCondition.getName());
        userConditionDTO.setAvatar(userCondition.getAvatar());
        userConditionDTO.setEmail(userCondition.getEmail());
        userConditionDTO.setPhone(userCondition.getPhone());
        if (userCondition.hasStatus()) {
            userConditionDTO.setStatus(userCondition.getStatusValue());
        }
        userConditionDTO.setContent(userCondition.getContent());
        if (userCondition.hasEdition()) {
            userConditionDTO.setEdition(userCondition.getEditionValue());
        }
        if (userCondition.hasShowGuide()) {
            userConditionDTO.setShowGuide(userCondition.getShowGuide() ? 1 : 0);
        }
        if (userCondition.hasShowVersion()) {
            userConditionDTO.setShowVersion(userCondition.getShowVersion() ? 1 : 0);
        }
        WxInfo wxInfo = userCondition.getWxInfo();
        userConditionDTO.setWxUnionid(wxInfo.getUnionId());
        userConditionDTO.setWxMiniOpenid(wxInfo.getMiniOpenId());
        userConditionDTO.setWxOffiOpenid(wxInfo.getOffiOpenId());
        userConditionDTO.setWxNickname(wxInfo.getNickname());
        FsInfo fsInfo = userCondition.getFsInfo();
        userConditionDTO.setFsUnionId(fsInfo.getUnionId());
        userConditionDTO.setFsOpenId(fsInfo.getOpenId());
        userConditionDTO.setFsName(fsInfo.getName());
        QyWxInfo qyWxInfo = userCondition.getQyWxInfo();
        userConditionDTO.setQyWxUserid(qyWxInfo.getUserId());
        userConditionDTO.setQyWxOpenUserid(qyWxInfo.getThirdUserId());
        userConditionDTO.setQyWxName(qyWxInfo.getName());
        DingInfo dingInfo = userCondition.getDingInfo();
        userConditionDTO.setDingUserid(dingInfo.getUserId());
        userConditionDTO.setDingUnionid(dingInfo.getUnionId());
        userConditionDTO.setDingName(dingInfo.getName());
        userConditionDTO.setSsoName(userCondition.getSsoName());
        userConditionDTO.setSilentId(userCondition.getSilentId());
        if (userCondition.hasAccept()) {
            userConditionDTO.setAcceptFlag(userCondition.getAccept() ? UserAccpectFlagEnum.ACCPECT.getType() : UserAccpectFlagEnum.NOT_ACCPECT.getType());
        }
        if (userCondition.hasLastLoginTime()) {
            userConditionDTO.setLastLoginTime(GrpcDateUtil.getDate(userCondition.getLastLoginTime()));
        }
        userConditionDTO.setRegisterIp(userCondition.getRegisterIp());
        if (userCondition.hasInner()) {
            userConditionDTO.setInnerFlag(userCondition.getInner() ? UserInnerFlagEnum.INNER.getType() : UserInnerFlagEnum.OUTTER.getType());
        }
        userConditionDTO.setShowGuideRecords(String.join(StrUtil.COMMA, userCondition.getGuideRecordsList()));
        if (userCondition.hasIndex()) {
            userConditionDTO.setUserIndex(userCondition.getIndexValue());
        }
        if (userCondition.hasBiShare()) {
            userConditionDTO.setBiShareFlag(BooleanStatusEnum.toInteger(userCondition.getBiShare()));
        }
        return userConditionDTO;
    }

    public static UserDO dto2Do(UserConditionDTO userConditionDTO) {
        if (Objects.isNull(userConditionDTO)) {
            return null;
        }
        UserDO userDO = new UserDO();
        userDO.setUserId(userConditionDTO.getUserId());
        userDO.setUserName(userConditionDTO.getUserName());
        userDO.setPassword(userConditionDTO.getPassword());
        userDO.setSalt(userConditionDTO.getSalt());
        userDO.setName(userConditionDTO.getName());
        userDO.setAvatar(userConditionDTO.getAvatar());
        userDO.setEmail(userConditionDTO.getEmail());
        userDO.setPhone(userConditionDTO.getPhone());
        userDO.setStatus(userConditionDTO.getStatus());
        userDO.setContent(userConditionDTO.getContent());
        userDO.setUserIndex(userConditionDTO.getUserIndex());
        userDO.setEdition(userConditionDTO.getEdition());
        userDO.setShowGuide(userConditionDTO.getShowGuide());
        userDO.setShowGuideRecords(userConditionDTO.getShowGuideRecords());
        userDO.setShowVersion(userConditionDTO.getShowVersion());
        userDO.setAcceptFlag(userConditionDTO.getAcceptFlag());
        userDO.setInnerFlag(userConditionDTO.getInnerFlag());
        userDO.setBiShareFlag(userConditionDTO.getBiShareFlag());
        userDO.setRegisterIp(userConditionDTO.getRegisterIp());
        userDO.setLastLoginTime(userConditionDTO.getLastLoginTime());
        userDO.setWxUnionid(userConditionDTO.getWxUnionid());
        userDO.setWxMiniOpenid(userConditionDTO.getWxMiniOpenid());
        userDO.setWxOffiOpenid(userConditionDTO.getWxOffiOpenid());
        userDO.setWxNickname(userConditionDTO.getWxNickname());
        userDO.setQyWxUserid(userConditionDTO.getQyWxUserid());
        userDO.setQyWxOpenUserid(userConditionDTO.getQyWxOpenUserid());
        userDO.setQyWxName(userConditionDTO.getQyWxName());
        userDO.setFsUnionId(userConditionDTO.getFsUnionId());
        userDO.setFsOpenId(userConditionDTO.getFsOpenId());
        userDO.setFsName(userConditionDTO.getFsName());
        userDO.setDingUserid(userConditionDTO.getDingUserid());
        userDO.setDingUnionid(userConditionDTO.getDingUnionid());
        userDO.setDingName(userConditionDTO.getDingName());
        userDO.setSsoName(userConditionDTO.getSsoName());
        userDO.setSilentId(userConditionDTO.getSilentId());
        userDO.setOrgId(userConditionDTO.getOrgId());
        return userDO;
    }
}
    